﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using FasterWPF;
using System.Windows.Controls.Primitives;

namespace FasterWPF
{
    public class UniformGridComposite: IComposite
    {
        public bool IsGrid()
        {
            return true;
        }

        public void RemoveFromChain(Border bdr) //unhook all the children so they may be re-added later, if desired
        {
            GetContainer<UniformGrid>(bdr).Children.Clear();
        }

        public C GetContainer<C>(Border bdr)
            where C: FrameworkElement
        {
           return ((C)(((ContentControl)(bdr.Child)).Content));
        }

        public C GetContainerFromChildContentControl<C>(ContentControl chain) where C : FrameworkElement
        {
           return ((C)(chain.Content));
        }

        //public void ShowGridLines(System.Windows.Controls.ContentControl chain)
        //{
        //    ((UniformGrid)(chain.Content)).ShowGridLines = true;
        //}

        public T GetParent<T>(ContentControl sender)
              where T : FrameworkElement
        {
            return (T)((ContentControl)sender).GetParentFromContentControlOfComposite<T>();
        }

        public Panel AddChild<T>(T source, System.Windows.Controls.ContentControl chain, int row, int col, System.Windows.FrameworkElement child)
               where T : FrameworkElement
        {
            UniformGrid g = ((UniformGrid)(chain.Content));
            g.Children.Add(child);

            return (Panel)g;
        }

        public void AddCompositeToContentControl<T>(T chain, ContentControl contentControl, object settings)
            where T: FrameworkElement
        {
            UniformGrid g = new UniformGrid();
            UniformGridSettings gs = null;

            if (settings != null)
            {
                gs = (settings as UniformGridSettings);
            }
            else
            {
                if (SettingsManager.ContainsSetting<T>(chain, "UniformGridSettings"))
                {
                    gs = SettingsManager.GetSetting<T, UniformGridSettings>(chain, "UniformGridSettings");
                }
                else
                {
                    //no default settings
                    throw new Exception("Uniform Grid Settings must be defined on Parent T.");
                }
            }
            applyGridSettings(gs, g);

            contentControl.Content = g;
        }

        private static UniformGrid applyGridSettings(UniformGridSettings gridSet, UniformGrid g)
        {
            g.Height = gridSet.Height;
            g.Width = gridSet.Width;
            g.Rows = gridSet.NumRows;
            g.Columns = gridSet.NumColumns;

            return g;
        }

        public T GetParentFromChild<T>(FrameworkElement childElement) where T : FrameworkElement
        {
            return (T)((ContentControl)((UniformGrid)((FrameworkElement)childElement.Parent)).Parent).GetParentFromContentControlOfComposite<T>();
        }

        public System.Windows.Controls.Border GetBorder<T>(System.Windows.FrameworkElement childElement)
        {
            return (Border)((UniformGrid)((FrameworkElement)childElement).Parent).Parent.Cast<ContentControl>().Parent;
        }
        
        public K Get<K, T>(System.Windows.Controls.ContentControl chain, int row, int column)
                where K : UIElement
                where T : FrameworkElement
        {
            return ((K)((UniformGrid)chain.Content).Children[column]);
        }

        public UIElementCollection GetChildren(object container)
        {
            return (container as UniformGrid).Children;
        }
    }
}
